package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.CreditTerm;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class AdminCreditTermBean extends AbstractBean {

	public static final String BEAN_NAME="adminCreditTermBean";
	public static final String NAV_LIST="adminCreditTermList";
	public static final String NAV_CREATE="adminCreditTermCreate";
	public static final String NAV_EDIT="adminCreditTermEdit";
	
	private MembershipService service;
	private CreditTerm term;
	private List<CreditTerm> termList;
	public CreditTerm getTerm() {
		return term;
	}
	public void setCreditTerm(CreditTerm term) {
		this.term = term;
	}

	public void setService(MembershipService service) {
		this.service = service;
	}
	/*------------------------------------------------------------*/
	public String navListAction(){
		return NAV_LIST;
	}
	
	public List<CreditTerm> getTermList() {
		try{
			termList = service.getCreditTermList();
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return termList;
	}
	
	public String navCreateAction(){
		term = new CreditTerm();
		return NAV_CREATE;
	}
	
	public String createAction(){
		try{
			service.createCreditTerm(term, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_CREATE;
	}
	
	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("termId");
		String id = (String)param.getValue();
		for(CreditTerm t:termList){
			if(t.getCreditTermId().equalsIgnoreCase(id)){
				term = t;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT;
	}
	
	public String editAction(){
		try{
			service.editCreditTerm(term, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_EDIT;
	}
	
	public String deleteAction(){
		try{
			service.deleteCreditTerm(termList, getThisUser());
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_LIST;
	}
}
